/**
 * 
 */
package base.mail;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.FromTerm;
import javax.mail.search.IntegerComparisonTerm;
import javax.mail.search.NotTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SentDateTerm;
import javax.mail.search.SizeTerm;
import javax.mail.search.SubjectTerm;
  
/** 
 * 搜索邮件 
 */  
public class SearchMailTest {  
      
    public static void main(String[] args) throws Exception {  
        Properties props = new Properties();  
        props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");
		props.put("mail.store.protocol", "imap");
		
		// [--------------- For gmail ---------------]
			/*props.put("mail.imap.host", "imap.gmail.com");
			props.put("mail.imap.port", 993);
			// SSL setting
			props.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			props.setProperty("mail.imap.socketFactory.fallback", "false");
			props.setProperty("mail.imap.socketFactory.port",String.valueOf(993));*/
		// [--------------- For gmail ---------------]
		
        Session session = Session.getInstance(props);  
        //URLName url = new URLName("imap", "imap.gmail.com", 993, null, "", "");  
        //Store store = session.getStore(url);  
        Store store =  session.getStore("imap"); 
        store.connect("la-email.corp.valueclick.com", "si-bireports", "X%$tjm9yy");
       // store.connect("imap.gmail.com", "boengood@gmail.com", "密码不告诉你");
       // store.connect();  
        // 得到收件箱  
        //Folder folder = store.getFolder("INBOX");  
        Folder folder = store.getFolder("INBOX");  
        //folder = folder.getFolder("trafficvance");
        // 以读写模式打开收件箱  
        folder.open(Folder.READ_ONLY);  
          
       // Message[] messages = search(folder);  
          
       // System.out.println("收件箱中共有:" + folder.getMessageCount() + "封邮件，搜索到" + messages.length + "封符合条件的邮件!");  
          
        // 解析邮件搜索到的邮件  
        //POP3ReceiveMailTest.parseMessage(messages);   
          
        String subject = "Campaigns Daily Report for Mezi";  
        String subjectJapan = "バリューコマース:ご注文データ添付_スマーター";  
        String subjectChina = "猎聘网";  
        SearchTerm subjectTerm = new SubjectTerm(subjectChina);  
    /*    String subject2 = "20121109";  
        SearchTerm subjectTerm2 = new SubjectTerm(subject2);  
       
        SearchTerm comparisonAndTerm = new AndTerm(subjectTerm, subjectTerm2); 
        
        SearchTerm subjectTerm3 = new SubjectTerm("smartercn");   
        SearchTerm comparisonAndTerm2 = new AndTerm(comparisonAndTerm, subjectTerm3);*/
        BodyTerm bodyTerm = new BodyTerm("ご担当者様");
        
        
        FromStringTerm fromTerm = new FromStringTerm("cssupport@valuecommerce.ne.jp");
        SearchTerm comparisonAndTerm = new AndTerm(subjectTerm, fromTerm); 
        
        Message[] searchedMessage =  folder.search(subjectTerm);
        System.out.println(searchedMessage.length);

        for (Message message : searchedMessage) {
        	
        	System.out.println(message.getSize());
			System.out.println(message.getSubject());
		}
        folder.close(true);  
        store.close();  
    }  
      
    public static Message[] search(Folder folder) throws Exception {  
        // 搜索主题包含美食的邮件  
        String subject = "java培训";  
        SearchTerm subjectTerm = new SubjectTerm(subject);  
          
        // 搜索发件人包含支付宝的邮件  
        SearchTerm fromTerm = new FromStringTerm("支付宝");  
          
        // 搜索邮件内容包含"招聘"的邮件  
        SearchTerm bodyTerm = new BodyTerm("招聘");  
          
        // 搜索发件人不包含“智联招聘”的邮件  
        SearchTerm notTerm = new NotTerm(new FromStringTerm("智联招聘"));  
          
        // 搜索发件人为“智联招聘”，而且内容包含“Java工程师“的邮件  
        SearchTerm andTerm = new AndTerm(  
                new FromStringTerm("智联招聘"),  
                new BodyTerm("java工程师"));  
          
          
        // 搜索发件人为”智联招聘“或主题包含”最新职位信息“的邮件  
        SearchTerm orTerm = new OrTerm(  
                new FromStringTerm("智联招聘"),   
                new SubjectTerm("最新职位信息"));  
          
  
        // 搜索周一到今天收到的的所有邮件  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK - (Calendar.DAY_OF_WEEK - 1)) - 1);  
        Date mondayDate = calendar.getTime();  
        SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);  
        SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());  
        SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);  
          
        // 搜索大于或等100KB的所有邮件  
        int mailSize = 1024 * 100;  
        SearchTerm intComparisonTerm = new SizeTerm(IntegerComparisonTerm.GE, mailSize);  
          
        return folder.search(intComparisonTerm);  
    }  
 }  